package Simulation;

import mathwork.Mathwork;
import fxana.FxRawDataSet;
import fxana.RawDataCell;

public class SimulatorLSM extends Simulator {
	public static int opt_numberofday=25;
	public static int opt_losscutval=24;

	public SimulatorLSM(){
		super();
	}
	
	@Override
	protected void ParameterOptimize(FxRawDataSet dataset,
			EvaluatorParaFlag pflag) {
		int n=0,on=0;
		int l=0,ol=0;
		Double MaxMerit=0.0;
		
		for(n=3;n<50;n++){
			for(l=5;l<35;l++){
				EvaluatorParaFlag  spflag=new EvaluatorParaFlag ();
				spflag.SetMFlag(n);
				spflag.SetLFlag(l);
				
				ConditionLSM seed=new ConditionLSM();
				SimulationSystem eval=new SimulationSystem(dataset,spflag,seed);
				eval.output();
				if(eval.GetTotalMerit().compareTo(MaxMerit)>0){
					on=n;
					ol=l;
					MaxMerit=eval.GetTotalMerit();
				}
				
				System.out.println("Temp MaxMerit:"+Mathwork.Roundup(MaxMerit,2)+" "
						+ "N:"+on+" "
						+ "L:"+ol);
				
			}
		}
		
		System.out.println("MaxMerit:"+Mathwork.Roundup(MaxMerit,2)+" "+ "is following:");
		EvaluatorParaFlag  spflag=new EvaluatorParaFlag ();
		spflag.SetMFlag(on);
		spflag.SetLFlag(ol);
		
		Condition seed;
		seed=new ConditionLSM();
		SimulationSystem eval=new SimulationSystem(dataset,spflag,seed);
		eval.output();
	}

	@Override
	protected void OneTest(FxRawDataSet dataset, EvaluatorParaFlag pflag) {
		ConditionLSM seed=new ConditionLSM();
		SimulationSystem eval=new SimulationSystem(dataset,pflag,seed);
		eval.output();
	}

	@Override
	protected void OutParameter(FxRawDataSet dataset, EvaluatorParaFlag pflag) {
		RawDataCell today=dataset.GetRawDataBuffer().lastElement();
		Double sellval=0.0;
		Double buyval=0.0;
		Double buylosscut=0.0;
		Double selllostcut=0.0;
		
		ConditionLSM seed=null;
		EvaluatorParaFlag  spflag=new EvaluatorParaFlag ();
		
		seed=new ConditionLSM();
		spflag.SetMFlag(SimulatorLSM.opt_numberofday);
		spflag.SetLFlag(SimulatorLSM.opt_losscutval);
		
		Mathwork.ifplot=true;
		RawDataCell est=seed.GetEstimatedValue(dataset, today,spflag);
		Mathwork.ifplot=false;
		if(est!=null){
			sellval=Mathwork.Roundup(est.GetHighValue(),2);
			selllostcut=Mathwork.Roundup(sellval+SimulatorLSM.opt_losscutval/10.0,2);
			buyval=Mathwork.Roundup(est.GetLowValue(),2);
			buylosscut=Mathwork.Roundup(buyval-SimulatorLSM.opt_losscutval/10.0,2);
		}else{
			System.out.println("Today can not be estimated!");
			sellval=selllostcut=buyval=buylosscut=0.0;
		}
		
		today.out();
		if(pflag.IsEFlagSet()){
			System.out.println("N: "+SimulatorLSM.opt_numberofday+" "+"L: "+SimulatorLSM.opt_losscutval/10.0);
		}
		System.out.println(today.GetFSRecDate()+" "+"Sellval: "+sellval+" "+"Buyval: "+buyval+" "+"SellLosscut: " + selllostcut+" "+"Buylosscut: "+buylosscut);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		SimulatorLSM sim=new SimulatorLSM();
		sim.DoSimulator(args);
	}

}
